package com.honey.mybatis.generator.dao.method;

import com.honey.compilation.java.AbstractJavaCompilation;
import com.honey.compilation.java.JavaVisibility;
import com.honey.compilation.java.MethodDecoration;
import com.honey.compilation.java.Parameter;
import com.honey.core.dbmapping.introspect.IntrospectSchema;
import com.honey.core.dbmapping.introspect.IntrospectSchemaColumn;
import com.honey.core.types.ExceptionFullyQualifiedJavaType;
import com.honey.core.types.FullyQualifiedJavaType;
import com.honey.core.types.JDKFullyQualifiedJavaType;
import com.honey.mybatis.generator.AbstractMethod;
import com.honey.mybatis.generator.MybatisPlugin;
import com.honey.mybatis.generator.dao.DaoConstant;

public class BaseDaoSelectOneForSqlMethod extends AbstractMethod {

	private static final FullyQualifiedJavaType RETURN_TYPE = new FullyQualifiedJavaType("java.util.Map<String, Object> "); 
	
	private static final String[] JAVA_DOC = new String[]{
		"自定定义sql语句查询,查询一条记录(不建议使用)。",
		"注意spring自动关闭数据库连接。",
	} ;
	

	private static final String[] ANNOTATION = new String[]{
		"Deprecated",
		"SuppressWarnings(\"unchecked\")",
	} ;
	
	
	private static final FullyQualifiedJavaType[] EXCEPTION = new FullyQualifiedJavaType[]{
		ExceptionFullyQualifiedJavaType.getExceptionInstance()
	}; 
	
	
	public BaseDaoSelectOneForSqlMethod(AbstractJavaCompilation clazz,
			MybatisPlugin belongPlugin, IntrospectSchema introspectSchema,
			IntrospectSchemaColumn introspectSchemaColumn) {
		super(clazz, belongPlugin, introspectSchema, introspectSchemaColumn);
	}

	/*
	 * (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getMethodName()
	 */
	@Override
	protected String getMethodName() {
		return DaoConstant.SELECT_ONE_FOR_SQL_METHOD_NAME;
	}

	/*
	 * (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getParameter()
	 */
	@Override
	protected Parameter[] getParameter() {
		return new Parameter[]{
			new Parameter(JDKFullyQualifiedJavaType.getStringInstance(),"sql","查询的sql语句,例如: select * from tableName where field_1=? and field_2=?"),
			new Parameter(JDKFullyQualifiedJavaType.getObjectInstance(),"...parameter","参数(用于装载sql语句中的?的值, 参数顺序必须和sql语句中?号的顺序相同)"),
		};
	}

	/*
	 * (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getJavaDoc()
	 */
	@Override
	protected String[] getJavaDoc() {
		return JAVA_DOC;
	}

	/*
	 * (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getMethodDecoration()
	 */
	@Override
	protected MethodDecoration getMethodDecoration() {
		MethodDecoration answer = super.getMethodDecoration();
		answer.setVisibility(JavaVisibility.PUBLIC);
		answer.setFinal(true);
		return answer;
		
//		return super.getDependentModel().getMethodDecoration(null);
	}

	/*
	 * (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getReturnType()
	 */
	@Override
	protected FullyQualifiedJavaType getReturnType() {
		return RETURN_TYPE;
	}

	/*
	 * (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getBody()
	 */
	@Override
	protected String[] getBody() {
		return new String[]{
				"SqlSession session = "+getInversionModel().call(null,null),
				"SqlRunner sqlRunner = new SqlRunner( session.getConnection() )",
				"	return (Map<String, Object>)sqlRunner.selectOne( sql, parameter);"
		};
	}

	/* (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getAnnotation()
	 */
	@Override
	protected String[] getAnnotation() {
		
		return ANNOTATION;
	}
	
	/* (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getException()
	 */
	@Override
	protected FullyQualifiedJavaType[] getException() {
		return EXCEPTION;
	}
}
